package com.hxd.otc.module.my.activity;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.api.Constant;
import com.hxd.otc.base.BaseActivity;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.my.contract.ModifyMoneyPasswordContract;
import com.hxd.otc.module.my.presenter.ModifyMoneyPasswordPresenter;
import com.hxd.otc.utils.DataUtils;
import com.hxd.otc.utils.UIUtils;
import com.hxd.otc.widget.TitleBar;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;

/**
 * Created by lzy on 2018/6/12 0012.
 * description ${修改交易密码}
 */
public class ModifyMoneyPasswordActivity extends BaseActivity<ModifyMoneyPasswordPresenter> implements ModifyMoneyPasswordContract.View {
    @BindView(R.id.title_bar)
    TitleBar mTitleBar;
    @BindView(R.id.tv_explain)
    TextView mTvExplain;
    @BindView(R.id.et_login_password)
    EditText mEtLoginPassword;
    @BindView(R.id.et_pay_password)
    EditText mEtPayPassword;
    @BindView(R.id.et_confirm_password)
    EditText mEtConfirmPassword;
    @BindView(R.id.et_phone_verification_code)
    EditText mEtPhoneVerificationCode;
    @BindView(R.id.btn_verification_code)
    TextView mBtnVerificationCode;
    @BindView(R.id.ll_verification_code)
    LinearLayout mLlVerificationCode;
    @BindView(R.id.view_line)
    View mViewLine;

    private Disposable mDisposable;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_modify_pay_password;
    }

    @Override
    protected void initView() {
        mTitleBar.setTitleText(getString(UserHelper.isSetPayPassword() ? R.string.my_reset_money_password : R.string.pay_password));
        mTvExplain.setHint(R.string.my_modify_money_password_explain);

        UIUtils.setLoginPasswordEditText(mEtLoginPassword);

        mEtPayPassword.setHint(getString(R.string.my_modify_money_password_require, Constant.PAY_PASSWORD_MIN_LENGTH, Constant.PAY_PASSWORD_MAX_LENGTH));
        UIUtils.setPayPasswordEditText(mEtPayPassword);

        mEtConfirmPassword.setHint(R.string.my_confirm_pay_password);
        UIUtils.setPayPasswordEditText(mEtConfirmPassword);
    }

    @Override
    protected ModifyMoneyPasswordPresenter createPresenter() {
        return new ModifyMoneyPasswordPresenter(this);
    }

    @OnClick({R.id.btn_confirm, R.id.btn_verification_code})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_confirm:
                onConfirm();
                break;
            case R.id.btn_verification_code:
                onGetVerCode();
                break;
        }
    }

    private void onConfirm() {

        String loginPassword = mEtLoginPassword.getText().toString().trim();
        if (!DataUtils.isLoginPassword(loginPassword)){
            mEtLoginPassword.requestFocus();
            return;
        }

        String payPassword = mEtPayPassword.getText().toString().trim();
        if (!DataUtils.isPayPassword(payPassword)) {
            mEtPayPassword.requestFocus();
            return;
        }

        String confirmPassword = mEtConfirmPassword.getText().toString().trim();
        if (!DataUtils.isPayPassword(confirmPassword, getString(R.string.my_confirm_pay_password))) {
            mEtConfirmPassword.requestFocus();
            return;
        }

        if (!payPassword.equals(confirmPassword)) {
            showToast(getString(R.string.two_input_password_not_sure));
            mEtConfirmPassword.requestFocus();
            return;
        }

        String verCode = mEtPhoneVerificationCode.getText().toString().trim();
        if (!DataUtils.isVerCode(verCode)) {
            mEtPhoneVerificationCode.requestFocus();
            return;
        }

        if (UserHelper.isSetPayPassword()) {
            mPresenter.resetMoneyPassword(loginPassword,payPassword, verCode);
        } else {
            mPresenter.setMoneyPassword(loginPassword,payPassword, verCode);
        }
    }

    /**
     * 获取验证码
     */
    private void onGetVerCode() {
        mBtnVerificationCode.measure(0, 0);
        mPresenter.getVerCode();
    }

    @Override
    public void startCountdown() {
        ViewGroup.LayoutParams layoutParams = mBtnVerificationCode.getLayoutParams();
        layoutParams.width = mBtnVerificationCode.getWidth();
        mBtnVerificationCode.setLayoutParams(layoutParams);
        mBtnVerificationCode.setEnabled(false);
        mDisposable = Flowable.intervalRange(0, 60, 0, 1, TimeUnit.SECONDS)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Consumer<Long>() {
                    @SuppressLint("DefaultLocale")
                    @Override
                    public void accept(Long aLong) throws Exception {
                        mBtnVerificationCode.setText(String.format("%ds", 60 - aLong));
                    }
                })
                .doOnComplete(new Action() {
                    @Override
                    public void run() throws Exception {
                        //倒计时完毕置为可点击状态
                        mBtnVerificationCode.setEnabled(true);
                        mBtnVerificationCode.setText(R.string.send_message_code);
                    }
                })
                .subscribe();
    }

    @Override
    public void resetMoneyPasswordSuccess() {
        showToast(getString(R.string.my_modify_money_password_success));
        finish();
    }

    @Override
    public void setMoneyPasswordSuccess() {
        showToast(getString(R.string.my_set_money_password_success));
        UserHelper.updateHasPayPassword(true);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDisposable != null && !mDisposable.isDisposed()) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void initEvent() {
    }
}
